package com.jp.knowledge.activity;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.jp.knowledge.R;
import com.jp.knowledge.broad.LocalBroadCast;
import com.jp.knowledge.callback.JpCallBack;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.logic.WebServiceLogic;
import com.jp.knowledge.webUtil.X5WebView;

import org.xutils.view.annotation.ViewInject;

public class WebActivity extends SlidingActivity {

    public static void gotoWebActivity(Context context, String url){
        gotoWebActivity(context, new Intent(), url);
    }

    public static void gotoWebActivity(Context context, Intent intent, String url){
        context.startActivity(intent.setClass(context, WebActivity.class)
                .putExtra("com.jp.web_activity_url", url));
    }

    @ViewInject(R.id.web_main_layout)
    private FrameLayout webLayout;

    private WebServiceLogic webServiceLogic;

    private X5WebView servicewebView;

    private LocalBroadcastManager localBroadcastManager;
    private LocalBroadCast localBroad;

    @Override
    protected int getResourceId() {
        return R.layout.activity_test;
    }

    @Override
    protected void init() {
        localBroadcastManager = LocalBroadcastManager.getInstance(mContext);
        webServiceLogic = new WebServiceLogic(mContext);
        webServiceLogic.setCallBack(new WebServiceLogic.WebLogicCallBack() {
            @Override
            public void done(X5WebView webView) {
                servicewebView =webView;
                webServiceLogic.setCallback(new JpCallBack() {
                    @Override
                    public void done() {
                        WebActivity.this.finish();
                    }
                });
                if(webView.getParent() != null)
                    ((ViewGroup)webView.getParent()).removeView(webView);
                webLayout.addView(webView);
            }
        });

        registerBroad();
    }

    /**
     * 注册广播
     */
    private void registerBroad() {
        localBroad = new LocalBroadCast(new LocalBroadCast.BroadCastCallBack() {
            @Override
            public void done(Context context, Intent intent) {
                String action = intent.getAction();
                if(action.equals(LocalBroadCast.WEB_DOC_DETAIL)){       // 跳到资讯详情
                    String param = intent.getStringExtra("param");
                    Intent in = new Intent(mContext, InfoDataDetailActivity.class);
                    in.putExtra("docParam",param);
                    startActivity(in);
                } else if(action.equals(LocalBroadCast.TO_SEARCH)){     // 跳到搜索
                    SearchSimpleActivity.gotoSearch(WebActivity.this, intent.getStringExtra("param"));
                } else if(action.equals(LocalBroadCast.TOGGLE_TIME)){   // 显示/隐藏状态来
                    String param = intent.getStringExtra("param");
                    if("\"0\"".equals(param))   // 隐藏
                        webLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
                    else                         // 显示
                        webLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                } else if(action.equals(LocalBroadCast.OPEN_URL_WITH_BROWSER)){
                    String url = intent.getStringExtra("param").replaceAll("\"", "");
                    WebBrowserActivity.openWebActivity(mContext, url);
                } else if(action.equals(LocalBroadCast.UPDATE_LIGHT_EXT)){
                    localBroadcastManager.sendBroadcast(new Intent(LocalBroadCast.GET_MY_EXPERIENCE_CODE));
                }
            }
        });
        IntentFilter filter = new IntentFilter();
        filter.addAction(LocalBroadCast.WEB_DOC_DETAIL);
        filter.addAction(LocalBroadCast.TO_SEARCH);
        filter.addAction(LocalBroadCast.TOGGLE_TIME);
        filter.addAction(LocalBroadCast.OPEN_URL_WITH_BROWSER);
        filter.addAction(LocalBroadCast.UPDATE_LIGHT_EXT);
        localBroadcastManager.registerReceiver(localBroad,filter);
    }

    @Override
    protected void onDestroy() {
        // 体验添加新产品，发送广播通知获取体验数据
        if("library/ProductList?recommendAction=exp".equals(webServiceLogic.getCurrentPath())) {
//            localBroadcastManager.sendBroadcast(new Intent(LocalBroadCast.GET_MY_EXPERIENCE_CODE));
            // 雷达
        } else if("competeTheme/buildTheme".equals(webServiceLogic.getCurrentPath())
                || "follow/companyList".equals(webServiceLogic.getCurrentPath())
                || "follow/productList".equals(webServiceLogic.getCurrentPath())
                || "follow/figureList".equals(webServiceLogic.getCurrentPath())
                || "follow/channelList".equals(webServiceLogic.getCurrentPath()))
            localBroadcastManager.sendBroadcast(new Intent(LocalBroadCast.GET_ATTION_DATA_BROAD));
        localBroadcastManager.unregisterReceiver(localBroad);
        webLayout.removeAllViews();
        ViewGroup view = (ViewGroup) getWindow().getDecorView();
        view.removeAllViews();
        webServiceLogic.unBind();
        Intent intent = new Intent(LocalBroadCast.GET_ATTION_DATA_BROAD);
        localBroadcastManager.sendBroadcast(intent);
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode != KeyEvent.KEYCODE_BACK)
            return super.onKeyDown(keyCode,event);
        if (servicewebView!=null
                && webLayout.getChildCount() > 0
                && servicewebView.canGoBack()) {
            servicewebView.loadUrl("javascript:windowNativeBridge.goBack()");
            return true;
        } else {
            finish();
        }
        return super.onKeyDown(keyCode,event);
    }
}
